package com.erp.myAdapter

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.erp.R
import com.erp.activity.DanjianivActivity
import com.erp.bean.PhotoBean
import com.erp.module.zhuanMaiDianTu.ZhuanMaiDianTuContract
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import com.erp.util.Sputil
import kotlinx.android.synthetic.main.lv_iv_item.view.*


class zhuanmaidianTuXxAdapter(private val xxv : ZhuanMaiDianTuContract.ZhuanMaiDianXXV ,private val mlist: List<PhotoBean>, private val mcontext: Context) : RecyclerView.Adapter<zhuanmaidianTuXxAdapter.ViewHolder>() {
    internal var width = 0
    init {
        width = Sputil.sp("erp").getInt("width", width)
    }

    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): zhuanmaidianTuXxAdapter.ViewHolder {
        val view: View = LayoutInflater.from(parent.getContext()).inflate(R.layout.lv_iv_item, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mlist.size+1
    }

    override fun onBindViewHolder(h: zhuanmaidianTuXxAdapter.ViewHolder, @SuppressLint("RecyclerView") position: Int) {
        val params = h.iv_item.layoutParams as LinearLayout.LayoutParams
        params.height = width / 3
        h.iv_item.layoutParams = params
        if (position == mlist.size) {
            h.tv_item.visibility=View.GONE
            h.iv_item.setImageResource(R.mipmap.erp_photo)
        } else {
            h.tv_item.visibility=View.VISIBLE
            Myutil.showhttpphoto(mlist[position].uri, h.iv_item, mcontext)
            if(mlist[position].fid == ""){
                h.tv_item.setTextColor(ContextCompat.getColor(mcontext!!,R.color.blue))
                h.tv_item.text = "未上传"
            }else{
                h.tv_item.setTextColor(ContextCompat.getColor(mcontext!!,R.color.black))
                Myutil.settv(Myutil.getdata(mlist[position].timestamp),h.tv_item)
            }
        }
        h.v.setOnClickListener(object : View.OnClickListener{
            override fun onClick(v: View?) {
                if (position == mlist.size) {
                    Dialogutil.photodialog(mcontext, null)
                } else {
                    val intent = Intent(mcontext, DanjianivActivity::class.java)
                    intent.putExtra("url", mlist[position].uri)
                    mcontext.startActivity(intent)
                }
            }

        })
        h.v.setOnLongClickListener(object : View.OnLongClickListener{
            override fun onLongClick(v: View?): Boolean {
                if(position < mlist.size){
                    xxv.delete(position)
                }
                return true
            }

        })

    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var iv_item : ImageView
        var tv_item : TextView
        var v: View
        init {
            v = view
            iv_item = view.iv_item
            tv_item = view.tv_time
        }
    }
}